package com.example.lotterysystem.common.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DirectRabbitConfig {
    public static final String EXCHANGE_NAME = "DirectExchange";
    public static final String QUEUE_NAME = "DirectQueue";
    public static final String ROUTING = "DirectRouting";

    public static final String DLX_EXCHANGE_NAME = "DlxDirectExchange";
    public static final String DLX_QUEUE_NAME = "DlxDirectQueue";
    public static final String DLX_ROUTING = "DlxDirectRouting";

    @Bean
    public Queue directQueue() {
//        return new Queue(QUEUE_NAME, true);
        // 正常队列绑定死信交换机
        return QueueBuilder.durable(QUEUE_NAME)
                .deadLetterExchange(DLX_EXCHANGE_NAME)
                .deadLetterRoutingKey(DLX_ROUTING).build();
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(EXCHANGE_NAME, true, false);
    }

    @Bean
    public Binding bindingDirect() {
        return BindingBuilder.bind(directQueue()).to(directExchange()).with(ROUTING);
    }

    /**
     * 死信队列 起名：DirectQueue
     * @return
     */
    @Bean
    public Queue dlxQueue() {
        return new Queue(DLX_QUEUE_NAME, true);
    }

    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange(DLX_EXCHANGE_NAME, true, false);
    }

    @Bean
    public Binding bindingDlx() {
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with(DLX_ROUTING);
    }
}